package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.heantai.easystruts.controller.Action;
import com.heantai.easystruts.form.ActionForm;

import dao.DaoFactory;
import dao.UserDao;
import entity.User;

public class LoginAction extends Action {

	@Override
	public String execute(ActionForm actionForm,HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String userName = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		UserDao dao = DaoFactory.getInstance(UserDao.class);
		User u = dao.findByUserName(userName);
		if (u == null || !u.getPassWord().equals(passWord)) {
			request.setAttribute("loginError", "用户不存在或密码错误");
			return "loginError";
		} else {
			HttpSession session = request.getSession();
			session.setAttribute("id", u.getId());
			return "userList";
		}
	}


}
